var currentPage = 1;

// 减少页数
function decreasePage() {
  if (currentPage > 1) {
    currentPage--;
    document.getElementById('page').innerText = currentPage;
  }
}

// 增加页数
function increasePage() {
  currentPage++;
  document.getElementById('page').innerText = currentPage;
}

var modal = document.getElementById("myModal");

function showModal() {
  modal.style.display = "block";
}

function hideModal() {
  modal.style.display = "none";
}

window.onclick = function (event) {
  if (event.target == modal) {
    hideModal();
  }
}

function updateRow() {
  var rowIndex = document.getElementById("rowIndex").value - 1; // 转换为索引
  var name = document.getElementById("name").value;
  var age = document.getElementById("age").value;
  var table = document.getElementById("myTable");
  var row = table.rows[rowIndex + 1]; // 跳过表头

  if (row) {
    row.cells[0].innerText = name;
    row.cells[1].innerText = age;
    hideModal();
  }
}

document.addEventListener("DOMContentLoaded", function () {
  var deleteBtn = document.getElementById("delete-btn");
  var deletePopup = document.getElementById("delete-popup");
  var confirmDeleteBtn = document.getElementById("confirm-delete");

  deleteBtn.addEventListener("click", function () {
    // 弹窗提示用户输入要删除的记录 ID
    var idToDelete = prompt("Enter the ID of the record you want to delete:");

    // 检查输入的 ID 是否有效
    if (idToDelete) {
      // 遍历表格行，找到 ID 匹配的行
      var rows = document.querySelectorAll("#data-table tbody tr");
      for (var i = 0; i < rows.length; i++) {
        var row = rows[i];
        var idCell = row.querySelector("td:first-child");
        if (idCell && idCell.textContent === idToDelete) {
          // 弹出确认删除的弹窗
          deletePopup.style.display = "block";
          // 将选中的行存储在变量中，以便后续删除
          var selectedRow = row;
          break;
        }
      }

      // 如果没有找到匹配的行，则提示用户
      if (!selectedRow) {
        alert("Record not found!");
      }
    }
  });

  document.querySelector(".close").addEventListener("click", function () {
    deletePopup.style.display = "none";
  });

  confirmDeleteBtn.addEventListener("click", function () {
    // 删除选中的行
    if (selectedRow) {
      selectedRow.remove();
    }
    deletePopup.style.display = "none";
  });
});
document.addEventListener("DOMContentLoaded", function () {
  var sortBtn = document.getElementById("sort-btn");
  var sortPopup = document.getElementById("sort-popup");
  var confirmSortBtn = document.getElementById("confirm-sort");

  sortBtn.addEventListener("click", function () {
    // 显示排序弹窗
    sortPopup.style.display = "block";
  });

  document.querySelector(".close").addEventListener("click", function () {
    sortPopup.style.display = "none";
  });

  confirmSortBtn.addEventListener("click", function () {
    // 调用排序函数
    sortTableById();
    // 隐藏排序弹窗
    sortPopup.style.display = "none";
  });
});

function sortTableById() {
  // 获取表格数据
  var rows = document.querySelectorAll("#data-table tbody tr");

  // 对数据进行排序
  rows.sort(function (a, b) {
    var idA = parseInt(a.querySelector("td:first-child").textContent);
    var idB = parseInt(b.querySelector("td:first-child").textContent);
    return idA - idB;
  });

  // 清空表格
  var tbody = document.querySelector("#data-table tbody");
  tbody.innerHTML = "";

  // 将排序后的数据添加到表格中
  for (var i = 0; i < rows.length; i++) {
    tbody.appendChild(rows[i]);
  }
}

document.addEventListener("DOMContentLoaded", function () {
  var paginateBtn = document.getElementById("paginate-btn");
  var paginatePopup = document.getElementById("paginate-popup");
  var confirmPaginateBtn = document.getElementById("confirm-paginate");

  paginateBtn.addEventListener("click", function () {
    // 显示分页弹窗
    paginatePopup.style.display = "block";
  });

  document.querySelector(".close").addEventListener("click", function () {
    paginatePopup.style.display = "none";
  });

  confirmPaginateBtn.addEventListener("click", function () {
    // 获取用户输入的页号和每页条数
    var pageNum = document.getElementById("page-num").value;
    var recordsPerPage = document.getElementById("records-per-page").value;

    // 验证输入是否有效
    if (pageNum && recordsPerPage) {
      // 调用分页函数
      paginateTable(pageNum, recordsPerPage);
      // 隐藏分页弹窗
      paginatePopup.style.display = "none";
    } else {
      alert("Please enter valid page number and records per page.");
    }
  });
});

function paginateTable(pageNum, recordsPerPage) {
  // 获取表格数据
  var rows = document.querySelectorAll("#data-table tbody tr");

  // 计算起始索引和结束索引
  var startIndex = (pageNum - 1) * recordsPerPage;
  var endIndex = startIndex + recordsPerPage;

  // 清空表格
  var tbody = document.querySelector("#data-table tbody");
  tbody.innerHTML = "";

  // 将指定页的数据添加到表格中
  for (var i = startIndex; i < endIndex && i < rows.length; i++) {
    tbody.appendChild(rows[i]);
  }
}
